{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Copyright &copy; 2012 Ondrej Martinsky, All rights reserved\n",
    "\n",
    "[www.quantandfinancial.com](http://www.quantandfinancial.com)\n",
    "# Binomial Option Pricing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using matplotlib backend: Qt4Agg\n",
      "Populating the interactive namespace from numpy and matplotlib\n"
     ]
    }
   ],
   "source": [
    "%pylab\n",
    "%matplotlib inline\n",
    "pylab.rcParams['figure.figsize'] = (10, 6)\n",
    "import scipy.optimize\n",
    "from pandas import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Definition of enumerators"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "call, put, european, american = 100, 101, 102, 103"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load prices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAmwAAAF/CAYAAAD0P5WNAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3XeYlNX5xvHvoSNFmtSlN2lSpCkKa4k9mmhM1Bh7SYz+\nojFGMTGgSdSQorFFo4gaQ4y9xK64RgKI0pGquPQFpClF2p7fH8+OOzs7Mzuz03fuz3Xtxc4777xz\nlmGYe5/TnPceEREREcletTLdABERERGJToFNREREJMspsImIiIhkOQU2ERERkSynwCYiIiKS5RTY\nRERERLJcSgObc26ic26Dc25+0LFxzrk1zrnZZV8nBd031jm33Dm32Dl3QirbJiIiIpIrXCrXYXPO\nHQXsAJ7w3h9Wdmwc8JX3/i8h5/YBJgPDgALgHaCn10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r7PVxI2q6gbwa5IXR2NJntrG2iTprhjYJE21qvoD+DLJD8AZ1v8N27qp\nY8ZfBt5I8n2SZeD5+1CmJG3JbT0kSZIa5x02SZKkxhnYJEmSGmdgkyRJapyBTZIkqXEGNkmSpMYZ\n2CRJkhpnYJMkSWqcgU2SJKlx/wI1Nt38ksFVpAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x24e4971db38>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "dataframe = pandas.read_csv('ivv_2012_11_05.csv', index_col=None, parse_dates=['date'])\n",
    "prices = array(dataframe['close'])[-250:]  # We will use last 250 trading days\n",
    "plot(prices), xlabel('time'), ylabel('price');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Calculation of daily continuous asset returns\n",
    "$$ r = log \\left( p_t - p_{t-1} \\right)$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "returns = log(prices[1:] / prices[:-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Calculation of daily and annualized volatility from daily returns\n",
    "$$ \\sigma_d^2 = E \\left[ \\left( X - E[X] \\right) ^2 \\right] $$\n",
    "$$ \\sigma^2 = \\sigma_d^2 \\times 250 $$ "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.14855482160262648"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "volat_d = numpy.std(returns)  # Daily volatility\n",
    "volat = sqrt(volat_d**2 * 250)  # Annualized volatility\n",
    "volat"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Calculation inputs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Calculation Inputs\n",
      "             Price : 142.410\n",
      "            Strike : 140.000\n",
      "         Risk-free : 0.001\n",
      "         Div Yield : 0.020\n",
      "          TTE Days : 46.000\n",
      "        Volatility : 0.149\n",
      "\n"
     ]
    }
   ],
   "source": [
    "side = call  # Option side\n",
    "style = american  # Option style\n",
    "price = prices[-1]  # Current instrument price (147.31, as of 2012/11/05)\n",
    "strike = 140  # Strike price\n",
    "riskfree = .0007  # Risk-free rate, Yield on 3m US Treasury Yields, as of 2012/11/05\n",
    "divyield = .0199  # Dividend yield on S&P 500 (IVV), as of 2012/11/05\n",
    "tte = (datetime(2012, 12, 22) - datetime(2012, 11, 6)).days  # Time to expiration in days\n",
    "\n",
    "print('Calculation Inputs')\n",
    "print('%18s : %0.3f' % ('Price', price))\n",
    "print('%18s : %0.3f' % ('Strike', strike))\n",
    "print('%18s : %0.3f' % ('Risk-free', riskfree))\n",
    "print('%18s : %0.3f' % ('Div Yield', divyield))\n",
    "print('%18s : %0.3f' % ('TTE Days', tte))\n",
    "print('%18s : %0.3f' % ('Volatility', volat))\n",
    "print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Pre-processing of inputs and calculation of per-step figures\n",
    "$$  u = e^{\\nu \\cdot \\sqrt{t_{\\bigtriangleup}}} \\; ; \\; d = \\frac{1}{u} $$\n",
    "\n",
    "$$ v = max \\left( \\frac{v_u \\cdot \\pi_u + v_d \\cdot \\pi_d}{e^{r_f \\cdot t_{\\bigtriangleup}}} \\; ; \\; p - s\\right) $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       Node prob U : 0.48722696\n",
      "       Node prob D : 0.51277304\n",
      "       Node tdelta : 0.01575342\n",
      "   Node discount f : 0.00001103\n"
     ]
    }
   ],
   "source": [
    "n = 8  # Depth of binomial tree (levels are numbered from 0 to n)\n",
    "tdelta = tte / (n * 365)  # Time delta per one step (as fraction of year)\n",
    "u = exp(volat * tdelta ** .5)  # Up movement per step\n",
    "d = 1 / u  # Down movement per step\n",
    "rf = exp(riskfree * tdelta) - 1  # Risk-free rate per step\n",
    "dy = exp(divyield * tdelta) - 1  # Dividend yield per step\n",
    "pu = (1 + rf - dy - d) / (u - d)  # Probability of up movement\n",
    "pd = 1 - pu  # Probability of down movement\n",
    "\n",
    "assert side in [call, put] and style in [american, european]\n",
    "print('%18s : %0.8f' % ('Node prob U', pu))\n",
    "print('%18s : %0.8f' % ('Node prob D', pd))\n",
    "print('%18s : %0.8f' % ('Node tdelta', tdelta))\n",
    "print('%18s : %0.8f' % ('Node discount f', rf))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Binomial Tree\n",
      "\n",
      "Node [8,0] \t Price 165.319 \t Option Value 25.319\n",
      "Node [8,1] \t Price 159.267 \t Option Value 19.267\n",
      "Node [8,2] \t Price 153.437 \t Option Value 13.437\n",
      "Node [8,3] \t Price 147.821 \t Option Value  7.821\n",
      "Node [8,4] \t Price 142.410 \t Option Value  2.410\n",
      "Node [8,5] \t Price 137.197 \t Option Value  0.000\n",
      "Node [8,6] \t Price 132.175 \t Option Value  0.000\n",
      "Node [8,7] \t Price 127.337 \t Option Value  0.000\n",
      "Node [8,8] \t Price 122.676 \t Option Value  0.000\n",
      "\n",
      "Node [7,0] \t Price 162.265 \t Option Value 22.265\n",
      "Node [7,1] \t Price 156.325 \t Option Value 16.325\n",
      "Node [7,2] \t Price 150.603 \t Option Value 10.603\n",
      "Node [7,3] \t Price 145.090 \t Option Value  5.090\n",
      "Node [7,4] \t Price 139.779 \t Option Value  1.174\n",
      "Node [7,5] \t Price 134.663 \t Option Value  0.000\n",
      "Node [7,6] \t Price 129.734 \t Option Value  0.000\n",
      "Node [7,7] \t Price 124.985 \t Option Value  0.000\n",
      "\n",
      "Node [6,0] \t Price 159.267 \t Option Value 19.267\n",
      "Node [6,1] \t Price 153.437 \t Option Value 13.437\n",
      "Node [6,2] \t Price 147.821 \t Option Value  7.821\n",
      "Node [6,3] \t Price 142.410 \t Option Value  3.082\n",
      "Node [6,4] \t Price 137.197 \t Option Value  0.572\n",
      "Node [6,5] \t Price 132.175 \t Option Value  0.000\n",
      "Node [6,6] \t Price 127.337 \t Option Value  0.000\n",
      "\n",
      "Node [5,0] \t Price 156.325 \t Option Value 16.325\n",
      "Node [5,1] \t Price 150.603 \t Option Value 10.603\n",
      "Node [5,2] \t Price 145.090 \t Option Value  5.391\n",
      "Node [5,3] \t Price 139.779 \t Option Value  1.795\n",
      "Node [5,4] \t Price 134.663 \t Option Value  0.279\n",
      "Node [5,5] \t Price 129.734 \t Option Value  0.000\n",
      "\n",
      "Node [4,0] \t Price 153.437 \t Option Value 13.437\n",
      "Node [4,1] \t Price 147.821 \t Option Value  7.930\n",
      "Node [4,2] \t Price 142.410 \t Option Value  3.547\n",
      "Node [4,3] \t Price 137.197 \t Option Value  1.018\n",
      "Node [4,4] \t Price 132.175 \t Option Value  0.136\n",
      "\n",
      "Node [3,0] \t Price 150.603 \t Option Value 10.613\n",
      "Node [3,1] \t Price 145.090 \t Option Value  5.683\n",
      "Node [3,2] \t Price 139.779 \t Option Value  2.250\n",
      "Node [3,3] \t Price 134.663 \t Option Value  0.565\n",
      "\n",
      "Node [2,0] \t Price 147.821 \t Option Value  8.085\n",
      "Node [2,1] \t Price 142.410 \t Option Value  3.922\n",
      "Node [2,2] \t Price 137.197 \t Option Value  1.386\n",
      "\n",
      "Node [1,0] \t Price 145.090 \t Option Value  5.950\n",
      "Node [1,1] \t Price 139.779 \t Option Value  2.622\n",
      "\n",
      "Node [0,0] \t Price 142.410 \t Option Value  4.244\n"
     ]
    }
   ],
   "source": [
    "print('Binomial Tree')\n",
    "# Generate terminal nodes of binomial tree\n",
    "level = []\n",
    "print()\n",
    "for j in range(0, n + 1):  # Iterate through nodes from highest to lowest price\n",
    "    # Instrument's price at the node\n",
    "    pr = price * d ** j * u ** (n - j)\n",
    "    # Option value at the node (depending on side)\n",
    "    ov = max(0.0, pr - strike) if side == call else max(0.0, strike - pr)\n",
    "    level.append((pr, ov))\n",
    "    print('Node [%i,%i] \\t Price %6.3f \\t Option Value %6.3f' % (n, j, pr, ov))\n",
    "\n",
    "levels = [None, None, None]  # Remember levels 0,1,2 for the greeks\n",
    "\n",
    "# reduce binomial tree\n",
    "for i in range(n - 1, -1, -1):  # [n-1 to 0]\n",
    "    levelNext = []\n",
    "    print()\n",
    "    for j in range(0, i + 1):  # Iterate through nodes from highest to lowest price\n",
    "        node_u, node_d = level[j], level[j + 1]\n",
    "        # Instrument's price at the node\n",
    "        pr = node_d[0] / d\n",
    "        # Option value at the node (depending on side)\n",
    "        ov = (node_d[1] * pd + node_u[1] * pu) / (1 + rf)\n",
    "        if style == american:  # American options can be exercised anytime\n",
    "            ov = max(ov, pr - strike if side == call else strike - pr)\n",
    "        levelNext.append((pr, ov))\n",
    "        print('Node [%i,%i] \\t Price %6.3f \\t Option Value %6.3f' % (i, j, pr, ov))\n",
    "    level = levelNext\n",
    "    if j <= 2: levels[j] = level  # save level 0,1,2 of the tree"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Read option value and greeks from the binomial tree\n",
    "\n",
    "**Delta** is the rate of change of option's value with respect to changes in underlying instrument's price. Looking to the figure above, this rate is derived from nodes 10 and 11 as follows:\n",
    "\n",
    "$$ \\delta =\\frac{v_{10} - v_{11}}{p_{10} - p_{11}} $$\n",
    "\n",
    "**Gamma** s a rate of change of option's delta with respect to changes in underlying instrument's price. Again, we first need to calculate deltas in nodes 10 and 11:\n",
    "$$ \\delta_{10} =\\frac{v_{20} - v_{21}}{p_{20} - p_{21}} \\; ; \\; \\delta_{11} =\\frac{v_{21} - v_{22}}{p_{21} - p_{22}} $$\n",
    "Gamma is then calculated as:\n",
    "$$ \\gamma = \\frac{\\delta_{10}-\\delta_{11}}{p_{10} - p_{11}}$$\n",
    "\n",
    "**Theta**\n",
    "\n",
    "$$ \\theta = \\frac{v_{21} - v_{00}}{2 \\times t_{\\bigtriangleup}} $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Results\n",
      "Option Value 4.244\n",
      "Delta 0.627\n",
      "Gamma 0.027\n",
      "Theta -10.194\n"
     ]
    }
   ],
   "source": [
    "optionvalue = levels[0][0][1]\n",
    "delta = (levels[1][0][1] - levels[1][1][1]) / (levels[1][0][0] - levels[1][1][0])\n",
    "delta1 = (levels[2][0][1] - levels[2][1][1]) / (levels[2][0][0] - levels[2][1][0])\n",
    "delta2 = (levels[2][1][1] - levels[2][2][1]) / (levels[2][1][0] - levels[2][2][0])\n",
    "gamma = (delta1 - delta2) / (levels[2][0][0] - levels[2][2][0])\n",
    "theta = (levels[2][1][1] - optionvalue) / (2 * tdelta)\n",
    "\n",
    "print()\n",
    "print('Results')\n",
    "print('Option Value %.03f' % optionvalue)\n",
    "print('Delta %.03f' % delta)\n",
    "print('Gamma %.03f' % gamma)\n",
    "print('Theta %.03f' % theta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [Root]",
   "language": "python",
   "name": "Python [Root]"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
